<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="3">
          <!-- 订单状态 -->
          <el-select
            v-model="value"
            clearable
            filterable
            placeholder="全部订单"
            @change="selectOrderChange"
            @clear="getOrderList"
          >
            <el-option
              v-for="item in orderStatus"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-col>
        <el-col :span="3">
          <!-- 评价状态 -->
          <el-select
            v-model="value2"
            clearable
            filterable
            placeholder="全部评价状态"
            @change="selectCommentChange"
            @clear="getOrderList"
          >
            <el-option
              v-for="item in commentStatus"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-col>
        <!-- 搜索订单id -->
        <el-col :span="3">
          <!-- 搜索框 -->
          <el-input
            placeholder="订单id"
            v-model="queryInfo.query"
            clearable
            @clear="getOrderList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 表单 -->
      <el-table
        ref="multipleTable"
        :data="orderList"
        tooltip-effect="dark"
        style="width: 100%"
        border
        stripe
      >
        <el-table-column prop="orderid" label="订单id"> </el-table-column>
        <el-table-column prop="paytype" label="支付方式">
          <template slot-scope="scope">
            <el-tag size="mini" v-if="scope.row.paytype == 0">微信支付</el-tag>
            <el-tag size="mini" v-if="scope.row.paytype == 1"
              >支付宝支付</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="totalprice" label="总金额(￥)">
        </el-table-column>
        <el-table-column prop="note" label="备注"> </el-table-column>
        <el-table-column prop="ispaid" label="订单状态">
          <template slot-scope="scope">
            <el-tag size="mini" v-if="scope.row.ispaid == 0">未支付</el-tag>
            <el-tag size="mini" v-if="scope.row.ispaid == 1">已支付</el-tag>
            <el-tag size="mini" v-if="scope.row.ispaid == 2">已取消</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="iscommented" label="评价状态">
          <template slot-scope="scope">
            <el-tag size="mini" v-if="scope.row.iscommented == 0"
              >未评价</el-tag
            >
            <el-tag size="mini" v-if="scope.row.iscommented == 1"
              >已评价</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="createtime" label="下单时间"> </el-table-column>
        <!-- 这里指定了一个宽度，才能撑下三个按钮 -->
        <el-table-column
          prop="address"
          label="操作"
          show-overflow-tooltip
          width="180px"
        >
          <!-- 这里也是使用了 插槽 -->
          <template slot-scope="scope">
            <el-button type="info" round @click="goDetail(scope.row)"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 3, 5]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 弹出框 -->
    <el-dialog title="订单详情" :visible.sync="orderDialogVisible">
      <!-- 基础信息 -->
      <el-divider content-position="left">基础信息</el-divider>
      <el-descriptions>
        <el-descriptions-item label="订单号">{{
          row.orderid
        }}</el-descriptions-item>
        <el-descriptions-item label="用户ID">{{
          row.userid
        }}</el-descriptions-item>
        <el-descriptions-item label="下单时间">{{
          row.createtime
        }}</el-descriptions-item>
        <el-descriptions-item label="支付方式">
          <el-tag size="small" v-if="row.paytype == 0">微信支付</el-tag>
          <el-tag size="small" v-if="row.paytype == 1">支付宝支付</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="用户备注">
          {{ row.note }}</el-descriptions-item
        >
        <el-descriptions-item label="总金额">{{
          row.totalprice
        }}</el-descriptions-item>
      </el-descriptions>
      <!-- 基础信息 -->
      <el-divider content-position="left">评价信息</el-divider>
      <el-descriptions v-if="row.commentdata != null">
        <el-descriptions-item label="总体评分">
          <el-rate v-model="row.commentdata.rate" disabled> </el-rate>
        </el-descriptions-item>
        <el-descriptions-item label="评价日期">{{
          row.commentdata.createtime
        }}</el-descriptions-item>
        <el-descriptions-item label="评价">{{
          row.commentdata.content
        }}</el-descriptions-item>
      </el-descriptions>

      <el-descriptions v-if="row.commentdata == null">
        <el-descriptions-item label="总体评分"> 暂无评价 </el-descriptions-item>
      </el-descriptions>
      <!-- 订单明细 -->
      <el-divider content-position="left">订单明细</el-divider>
      <el-table :data="row.orderdata" stripe style="width: 100%">
        <el-table-column prop="dishpic" label="图片" width="180">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.dishpic"
              ></el-image>
          </template>

        </el-table-column>
        <el-table-column prop="dishname" label="商品" width="180">
        </el-table-column>
        <el-table-column prop="count" label="数量"> </el-table-column>
        <el-table-column prop="dishorder" label="单价(￥)"> </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>




<script>
export default {
  data() {
    return {
      value: "",
      value2: "",
      orderList: [],
      queryInfo: {
        query: "",
        //当前的页数
        pagenum: 1,
        //当前每页显示多少条数据
        pagesize: 5,
      },
      orderDialogVisible: false,
      //每一行的数据
      row: {},
      total: 0,
      orderStatus: [
        {
          id: 0,
          name: "未付款",
        },
        {
          id: 1,
          name: "已付款",
        },
        {
          id: 2,
          name: "已取消",
        },
      ],
      commentStatus: [
        {
          id: 0,
          name: "未评价",
        },
        {
          id: 1,
          name: "已评价",
        },
      ],
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    selectOrderChange() {
      console.log(this.value, "订单选中状态变化");
      this.getOrderList();
    },
    selectCommentChange() {
      console.log(this.value2, "评价状态改变");
      this.getOrderList();
    },
    async getOrderList() {
      const { data: res } = await this.$http.post(
        `/order/page/${this.queryInfo.pagenum}/${this.queryInfo.pagesize}`,
        {
          ispaid: this.value,
          iscommented: this.value2,
          orderid: this.queryInfo.query,
        }
      );
      console.log(res, "分页查询到的所有数据");
      this.orderList = res.data.records;
      this.total = res.data.total;
    },
    async goDetail(row) {
      console.log(row, "我是row");
      this.orderDialogVisible = true;
      const { data: res } = await this.$http.get(
        `/comment/getByOrderid/${row.orderid}`
      );
      console.log(res, "根据orderid查找出的评论");
      if (res.flag == true) {
        let key2 = "commentdata";
        let commentdata = res.data;
        row[key2] = commentdata;
      }

      let key = "orderdata";
      let orderdata = JSON.parse(row.data);
      row[key] = orderdata;
      this.row = row;
    },
    //监听 pageesize 改变的事件
    handleSizeChange(newSize) {
      console.log(`每页 ${newSize} 条`);
      this.queryInfo.pagesize = newSize;
      this.getOrderList();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(`当前页: ${newPage}`);
      this.queryInfo.pagenum = newPage;
      this.getOrderList();
    },
  },
};
</script>

<style scoped>
.el-pagination {
  margin-top: 15px;
}
.el-card {
  margin: 15px 0;
}
.el-table {
  margin-top: 15px;
}
</style>